package haojie.springbootdemo.task;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import haojie.springbootdemo.service.EmployeeService;
import haojie.springbootdemo.utils.time.TimeUtil;
import haojie.springbootdemo.entity.QueryLog;
import haojie.springbootdemo.repository.QueryLogRepository;
import java.time.ZonedDateTime;

@Service
public class DemoTask {
    private static final Logger log = LoggerFactory.getLogger(DemoTask.class);

    private static final long FIXED_RATE_MS = 10_000;
    private static final long INITIAL_DELAY_MS = 3_000;
    private static final String WORKDAY_CRON = "0 15 9 ? * MON-FRI";

    private final EmployeeService employeeService;
    private final QueryLogRepository queryLogRepository;

    public DemoTask(EmployeeService employeeService, QueryLogRepository queryLogRepository) {
        this.employeeService = employeeService;
        this.queryLogRepository = queryLogRepository;
    }

    /**
     * Fetches data at fixed intervals regardless of previous execution completion
     */
    @Scheduled(fixedRate = FIXED_RATE_MS)
    public void fetchDataWithFixedRate() {
        log.info("Starting fixed rate data fetch");
        long startTime = System.currentTimeMillis();

        try {
            String employeeName = employeeService.getFirstEmployeeName();
            long endTime = System.currentTimeMillis();
            long executionTime = endTime - startTime;

            log.info("First employee name: {}", employeeName);
            log.info("Query executed in {} ms", executionTime);

            // Save query log
            QueryLog logEntry = new QueryLog();
            logEntry.setCreateTime(System.currentTimeMillis());
            logEntry.setQueryType("FIXED_RATE_QUERY");
            logEntry.setExecutionTime(executionTime);
            logEntry.setQueryResult(employeeName);
            queryLogRepository.save(logEntry);

        } catch (Exception e) {
            long endTime = System.currentTimeMillis();
            long executionTime = endTime - startTime;

            log.error("Error executing query: {}", e.getMessage());

            // Save failed query log
            QueryLog logEntry = new QueryLog();
            logEntry.setCreateTime(System.currentTimeMillis());
            logEntry.setExecutionTime(executionTime);
            logEntry.setQueryResult("ERROR: " + e.getMessage());
            logEntry.setQueryType("FIXED_RATE_QUERY");
            queryLogRepository.save(logEntry);
        }
    }

    /**
     * Fetches data with fixed delay between completion of previous execution
     */
    @Scheduled(fixedDelay = FIXED_RATE_MS, initialDelay = INITIAL_DELAY_MS)
    public void fetchDataWithFixedDelay() {
        log.info("Starting fixed delay data fetch");

        // Add actual data fetching logic here
    }

    /**
     * Fetches data on workdays at 9:15 AM
     */
    @Scheduled(cron = WORKDAY_CRON)
    public void fetchDataWithCron() {
        log.info("Starting scheduled workday data fetch");

        // Add actual data fetching logic here
    }
}
